package models;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.*;

import models.Transaction.Status;

import java.util.Date;

/**
 * The persistent class for the wm$transaction database table.
 * 
 */
@Entity
@Table(name = "WM$TRANSACTION")
public class WmTransaction implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(
            name = "TRANS_SEQ",
            table = "SEQUENCE",
            pkColumnName = "NAME",
            pkColumnValue = "TRANS_SEQ",
            valueColumnName = "CNT",
            initialValue = 10000,
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "TRANS_SEQ")
    @Column(unique = true, nullable = false)
    private Integer id;

    private BigDecimal amount;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "COMPLETED_D")
    private Date completedD;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_D")
    private Date createD;

    @Column(name = "FROM_WALLET_BALANCE")
    private BigDecimal fromWalletBalance;

    @Column(name = "TO_WALLET_BALANCE")
    private BigDecimal toWalletBalance;

    private int type;

    private int status;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATE_D")
    private Date updateD;

    @Column(name = "FROM_WALLET")
    private Integer fromWalletId;

    @Column(name = "TO_WALLET")
    private Integer toWalletId;

    public WmTransaction() {
    }

    public WmTransaction(Integer fromWallet, Integer toWallet, BigDecimal amount, Type type) {
        super();
        this.fromWalletId = fromWallet;
        this.toWalletId = toWallet;
        this.status = Status.PENDING.ordinal();
        this.type = type.ordinal();
        this.amount = amount;
        this.createD = new Date();
        this.updateD = new Date();
    }

    public void setValues(Status status, BigDecimal fromWalletB, BigDecimal toWalletB) {
        setStatus(status);
        setFromWalletBalance(fromWalletB);
        setToWalletBalance(toWalletB);
        setCompletedD(new Date());
        setUpdateD(new Date());
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public BigDecimal getAmount() {
        return this.amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public Date getCompletedD() {
        return this.completedD;
    }

    public void setCompletedD(Date completedD) {
        this.completedD = completedD;
    }

    public Date getCreateD() {
        return this.createD;
    }

    public void setCreateD(Date createD) {
        this.createD = createD;
    }

    public BigDecimal getFromWalletBalance() {
        return this.fromWalletBalance;
    }

    public void setFromWalletBalance(BigDecimal fromWalletBalance) {
        this.fromWalletBalance = fromWalletBalance;
    }

    public BigDecimal getToWalletBalance() {
        return this.toWalletBalance;
    }

    public void setToWalletBalance(BigDecimal toWalletBalance) {
        this.toWalletBalance = toWalletBalance;
    }

    public int getType() {
        return this.type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Date getUpdateD() {
        return this.updateD;
    }

    public void setUpdateD(Date updateD) {
        this.updateD = updateD;
    }

    public Status getStatus() {
        return Status.values()[this.status];
    }

    public void setStatus(Status status) {
        this.status = status.ordinal();
    }

    public Integer getFromWalletId() {
        return fromWalletId;
    }

    public void setFromWalletId(Integer fromWalletId) {
        this.fromWalletId = fromWalletId;
    }

    public Integer getToWalletId() {
        return toWalletId;
    }

    public void setToWalletId(Integer toWalletId) {
        this.toWalletId = toWalletId;
    }

    public enum Status {
        NEW, // 0
        PENDING, // 1
        EXECUTED, // 2
        FAILED, // 3
        ROLLBACK, // 4
    }

    public enum Type {
        LOAD, // 0
        WITHDRAW, // 1
        TRANSFER, // 2
        FUNDS_TO_SYSTEM //3
    }

}